{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bd2b2eba-b7fd-4856-960f-f2cbadcc12af",
   "metadata": {},
   "source": [
    "# Building a Tavily Data Agent\n",
    "\n",
    "<a href=\"https://colab.research.google.com/github/run-llama/llama-hub/blob/main/llama_hub/tools/notebooks/tavily.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
    "\n",
    "This tutorial walks through using the LLM tools provided by the [Tavily API](https://app.tavily.com/) to allow LLMs to easily search and retrieve relevant content from the Internet.\n",
    "\n",
    "To get started, you will need an [OpenAI api key](https://platform.openai.com/account/api-keys) and a [Tavily API key](https://app.tavily.com)\n",
    "\n",
    "We will import the relevant agents and tools and pass them our keys here:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6b2678c-09a5-4bf5-b45a-0543d2c100f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index llama-hub tavily-python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ce6c6e5d-f61e-461e-8e8d-99d27fb3fcaf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-25T09:55:23.881158Z",
     "start_time": "2023-10-25T09:55:23.865840Z"
    }
   },
   "outputs": [],
   "source": [
    "# Set up OpenAI\n",
    "import openai\n",
    "from llama_index.agent import OpenAIAgent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c90f5ea-9ed0-43c5-a658-1a3d93382eaa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-25T09:55:23.881158Z",
     "start_time": "2023-10-25T09:55:23.865840Z"
    }
   },
   "outputs": [],
   "source": [
    "openai.api_key = \"sk-api-key\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3ab26eee-63ed-42ec-972a-419a9a5c1d82",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-25T09:55:23.881158Z",
     "start_time": "2023-10-25T09:55:23.865840Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "search\n"
     ]
    }
   ],
   "source": [
    "# Set up Tavily tool\n",
    "from llama_hub.tools.tavily_research.base import TavilyToolSpec\n",
    "\n",
    "tavily_tool = TavilyToolSpec(\n",
    "    api_key=\"tvly-api-key\",\n",
    ")\n",
    "\n",
    "tavily_tool_list = tavily_tool.to_tool_list()\n",
    "for tool in tavily_tool_list:\n",
    "    print(tool.metadata.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe8e3012-bab0-4e55-858a-e3721282552c",
   "metadata": {},
   "source": [
    "## Testing the Tavily search tool\n",
    "\n",
    "We've imported our OpenAI agent, set up the api key, and initialized our tool. Let's test out the tool before setting up our Agent.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e64da618-b4ab-42d7-903d-f4eeb624f43c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-25T09:55:40.329249Z",
     "start_time": "2023-10-25T09:55:25.783993Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(id_='47fad889-3ced-4a91-bc4b-8af9939c6535', embedding=None, metadata={'url': 'https://www.vox.com/culture/2023/9/6/23861675/burning-man-2023-mud-stranded-climate-change-playa-foot'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, hash='879353cf1df00d8e20618086e44683d83ed17bf093524e2ef60ac111291d6094', text='Has Burning Man finally lost its glamour?\\n\\nGive\\nNewsletters\\nSite search\\nVox main menu\\nFiled under:\\nThe Burning Man flameout, explained\\nClimate change — and schadenfreude\\xa0— finally caught up to the survivalist cosplayers.\\nShare this story\\nShare\\n\\nSeptember 1, after most of the scheduled events and live performances were canceled due to the weather, Burning Man organizers closed routes in and out of the area, forcing attendees to stay behind\\n\\nShare\\nAll sharing options for:\\nThe Burning Man flameout, explained\\n\\nFor most of its existence, Burning Man, the week-long desert experience that’s part utopian performance, part survival quest, and part drug trip, has dodged serious cultural pushback, beyond eye', start_char_idx=None, end_char_idx=None, text_template='{metadata_str}\\n\\n{content}', metadata_template='{key}: {value}', metadata_seperator='\\n'),\n",
       " Document(id_='e26008c2-508e-42bc-b7bb-5e75ba8d450b', embedding=None, metadata={'url': 'https://www.insider.com/photos-what-it-was-really-like-at-burning-man-2023-2023-9'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, hash='612b38bc411db0a05acd7dc5e9cc335743e307427527e5cb5f25aeb57a8bbeb6', text='I\\'ve seen Burning Man 2023 called a \"total disaster\" and \"Fyre Festival 2.0.\" I\\'ve read stories about people \"clamoring to escape\" and leaving behind their cars and trash.\\n\\nMy first days at Burning Man featured plenty of parties.\\nBurning Man is known for its incredible art and music, and we spent the first few days exploring as much of it as possible.\\n\\nI went to Burning Man for the first time and it was one of the best weeks of my life.\\n\\nThe bulk of my incredible Burning Man experience was due to Android Oasis, once a six-person crew of friends that has since blossomed into a 70-person camp.\\n\\nI found myself dancing in everything from a towering pyramid to a fluffy cloud.\\nLife at Burning Man often felt like living in a video game —\\xa0or a fairy tale.', start_char_idx=None, end_char_idx=None, text_template='{metadata_str}\\n\\n{content}', metadata_template='{key}: {value}', metadata_seperator='\\n'),\n",
       " Document(id_='83b1399d-b9d9-4f7c-9351-49666785e4a4', embedding=None, metadata={'url': 'https://www.insider.com/photos-what-it-was-really-like-at-burning-man-2023-2023-9'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, hash='612b38bc411db0a05acd7dc5e9cc335743e307427527e5cb5f25aeb57a8bbeb6', text='I\\'ve seen Burning Man 2023 called a \"total disaster\" and \"Fyre Festival 2.0.\" I\\'ve read stories about people \"clamoring to escape\" and leaving behind their cars and trash.\\n\\nMy first days at Burning Man featured plenty of parties.\\nBurning Man is known for its incredible art and music, and we spent the first few days exploring as much of it as possible.\\n\\nI went to Burning Man for the first time and it was one of the best weeks of my life.\\n\\nThe bulk of my incredible Burning Man experience was due to Android Oasis, once a six-person crew of friends that has since blossomed into a 70-person camp.\\n\\nI found myself dancing in everything from a towering pyramid to a fluffy cloud.\\nLife at Burning Man often felt like living in a video game —\\xa0or a fairy tale.', start_char_idx=None, end_char_idx=None, text_template='{metadata_str}\\n\\n{content}', metadata_template='{key}: {value}', metadata_seperator='\\n')]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tavily_tool.search(\"What happened in the latest Burning Man festival?\", max_results=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1210906d-87a7-466a-9712-1d17dba2c2ec",
   "metadata": {},
   "source": [
    "### Using the Search tool in an Agent\n",
    "\n",
    "We can create an agent with access to the Tavily search tool start testing it out:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9d88c2ee-184a-4371-995b-a086b34db24f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-25T09:55:48.339621Z",
     "start_time": "2023-10-25T09:55:48.332915Z"
    }
   },
   "outputs": [],
   "source": [
    "agent = OpenAIAgent.from_tools(\n",
    "    tavily_tool_list,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f69a53fd-55c4-4e18-8fbe-6a29d5f3cef0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-25T09:55:57.109392Z",
     "start_time": "2023-10-25T09:55:50.456904Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Analysis of the Latest Burning Man Floods\n",
      "\n",
      "## Introduction\n",
      "The Burning Man festival, held annually in the Black Rock Desert of Nevada, is known for its unique art installations, music performances, and community spirit. However, in recent years, the festival has faced challenges due to extreme weather conditions, including floods. This analysis aims to provide a deep understanding of the latest Burning Man floods, their causes, impacts, and the response of the festival organizers.\n",
      "\n",
      "## Overview of the Latest Burning Man Floods\n",
      "According to a [news article](https://www.today.com/news/what-is-burning-man-flood-death-rcna103231), the latest Burning Man festival experienced heavy rains that resulted in muddy conditions, making it difficult for festivalgoers to leave the Nevada desert. The article mentions that tens of thousands of festival attendees were slowly making their way home as the muddy conditions made it nearly impossible to leave over the weekend. The festival organizers provided a \"Wet Playa Survival Guide\" with tips to those still on-site, indicating that the conditions were improving.\n",
      "\n",
      "## Causes of the Floods\n",
      "1. Heavy Rainfall: The primary cause of the floods at Burning Man was heavy rainfall. The article mentions that the festival experienced significant rain, leading to muddy conditions and making it challenging for attendees to navigate the desert.\n",
      "2. Flash Floods: The Black Rock Desert is prone to flash floods due to its flat topography and lack of vegetation. When rainwater accumulates faster than it can be absorbed or evaporated, it quickly forms torrents that flood the festival grounds.\n",
      "\n",
      "## Impacts of the Floods\n",
      "1. Transportation Challenges: The muddy conditions caused by the floods made it difficult for festivalgoers to leave the desert. This created transportation challenges and delays for tens of thousands of attendees.\n",
      "2. Disruption of Activities: The floods disrupted the planned activities and performances at Burning Man. The muddy conditions may have made it challenging for artists and performers to showcase their work, impacting the overall experience of the festival.\n",
      "3. Safety Risks: The muddy and slippery conditions posed safety risks to festival attendees. The article does not mention any specific incidents, but the increased risk of slips, falls, and injuries is a concern during such weather conditions.\n",
      "\n",
      "## Response of the Festival Organizers\n",
      "The article does not provide detailed information about the specific response of the festival organizers to the floods. However, it mentions that the festival organizers provided a \"Wet Playa Survival Guide\" with tips to those still on-site, indicating that they were actively addressing the challenges posed by the floods. It is likely that the organizers were monitoring the situation, providing updates, and taking necessary measures to ensure the safety and well-being of the attendees.\n",
      "\n",
      "## Conclusion\n",
      "The latest Burning Man floods, caused by heavy rainfall and resulting in muddy conditions, created challenges for festivalgoers and disrupted the planned activities at the event. The transportation difficulties, disruption of activities, and safety risks highlight the impact of extreme weather events on the festival. While the specific response of the festival organizers is not detailed in the available information, it can be assumed that they took measures to address the challenges and ensure the safety of the attendees. As extreme weather events become more frequent and intense, it is crucial for the festival organizers to continue evaluating and implementing strategies to mitigate the impact of floods and other weather-related challenges at Burning Man.\n"
     ]
    }
   ],
   "source": [
    "print(\n",
    "    agent.chat(\n",
    "        \"Write a deep analysis in markdown syntax about the latest burning man floods\"\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "555c6c1a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama_hub",
   "language": "python",
   "name": "llama_hub"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
